package com.admin.base;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonInclude.Include;

@JsonInclude(Include.NON_NULL)
public class Response {

	public static final Response DEFAULT_ERROR = Response.error(-1, "服务失败");

	private Object data;
	private int code;
	private String msg;
	private long count;

	public Response() {
		super();
	}

	public Object getData() {
		return data;
	}

	public void setData(Object data) {
		this.data = data;
	}

	public int getCode() {
		return code;
	}

	public void setCode(int code) {
		this.code = code;
	}

	public String getMsg() {
		return msg;
	}

	public void setMsg(String msg) {
		this.msg = msg;
	}

	public long getCount() {
		return count;
	}

	public void setCount(long count) {
		this.count = count;
	}

	@Override
	public String toString() {
		return "Response [data=" + data + ", code=" + code + ", msg=" + msg + "]";
	}

	/**
	 * 无返回值
	 * @return
	 */
	public static Response ok() {
		Response res = new Response();
		res.code = 0;
		res.msg = "success";
		return res;
	}

	/**
	 * 一般数据结果
	 * @param result
	 * @return
	 */
	public static Response ok(Object result) {
		Response res = new Response();
		res.data = result;
		res.code = 0;
		res.msg = "success";
		return res;
	}

  /**
   * 分页结果
   * @param page
   * @return
   */
	public static Response ok(Page<?> page) {
		Response res = new Response();
		res.data = page.getResult();
		res.code = 0;
		res.msg = "success";
		res.count = page.getTotalRecord();
		return res;
	}

	/**
	 * 无返回值，自定义返回状态和信息
	 * @param code
	 * @param msg
	 * @return
	 */
	public static Response ok(int code, String msg) {
		Response res = new Response();
		res.code = code;
		res.msg = msg;
		return res;
	}

	/**
	 * 无返回值，枚举返回状态和信息
	 * @param responseType
	 * @return
	 */
	public static Response ok(ResponseType responseType) {
		Response res = new Response();
		res.code = responseType.getCode();
		res.msg = responseType.getMsg();
		return res;
	}

	/**
	 * 无返回值，自定义返回状态和信息
	 * @param code
	 * @param msg
	 * @return
	 */
	public static Response error(int code, String msg) {
		Response res = new Response();
		res.code = code;
		res.msg = msg;
		return res;
	}

}
